<?php
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Classes\Util\HTTP\Session;
use \PsiFramework\Classes\Util\UI\Printer;
use \PsiFramework\Classes\Util\UI\HTML;
use \PsiFramework\Plugins\Form\Classes\DAO\FormResponseDAO;
use \PsiFramework\Plugins\Form\Classes\Util\FormUtil;

import("Plugins.Form.Classes.DAO.FormResponseDAO");
import("Plugins.Form.Classes.Util.FormUtil");

$user = $response->getAttribute("user");
$entry = $response->getAttribute("entry");
$category = $response->getAttribute("category");
if (is_null($category) && !is_null($entry)) $category = $entry->getCategory();

$categoryLink = '' ;
if (!is_null($category)) $categoryLink = '&category_id=' . $category->getId();
?>
<article id="cont-princ" class="nocols">
    <div class="bloc bloc-2">
        <h2>
            <?php Printer::display($languageManager->getText('SEE_YOUR_ENTRY')); ?>
        </h2>
        <?php 
        if (!is_null($entry) && !is_null($entry->getCategory())) {
        ?>
        <div class="entry">
            <h3>
                <?php Printer::display($languageManager->getText('GENERAL_INFORMATION')); ?>
                <?php
                echo HTML::link(
                    $languageManager->getText('MODIFY_ENTRY'),
                    "action=EditProfile&entry_id=".$entry->getId().'&form_id=entry'.$categoryLink,
                    array('class'=>'edit')
                );
                ?>
            </h3>
            <ul>
                <li>
                    <strong>
                        <?php Printer::display($languageManager->getText('NAME')) ?>
                    </strong>
                     : <?php Printer::display($entry->getName()); ?>
                 </li>
                <li>
                    <strong>
                        <?php Printer::display($languageManager->getText('ADDRESS')) ?>
                    </strong>
                     : <?php Printer::display($entry->getAddress()); ?>
                </li>
                <li>
                    <strong>
                        <?php Printer::display($languageManager->getText('CITY')) ?>
                    </strong>
                     : <?php Printer::display($entry->getCity()); ?>
                </li>
                <li>
                    <strong>
                        <?php Printer::display($languageManager->getText('ZIP')) ?>
                    </strong>
                     : <?php Printer::display($entry->getZip()); ?>
                </li>
                <li>
                    <strong>
                        <?php Printer::display($languageManager->getText('TELEPHONE')) ?>
                    </strong>
                     : <?php Printer::display($entry->getTel()); ?>
                </li>
                <?php 
                if (!isNullOrEmptyString($entry->getFax())) {
                ?>
                <li>
                    <strong>
                        <?php Printer::display($languageManager->getText('FAX')) ?>
                    </strong>
                     : <?php Printer::display($entry->getFax()); ?>
                </li>
                <?php 
                }
                
                if (!isNullOrEmptyString($entry->getMobile())) {
                ?>
                <li>
                    <strong>
                        <?php Printer::display($languageManager->getText('MOBILE')) ?>
                    </strong>
                     : <?php Printer::display($entry->getMobile()); ?>
                </li>
                <?php 
                }
                
                if (!isNullOrEmptyString($entry->getTel2())) {
                ?>
                <li>
                    <strong>
                        <?php Printer::display($languageManager->getText('TELEPHONE2')) ?>
                    </strong>
                     : <?php Printer::display($entry->getTel2()); ?>
                </li>
                <?php 
                }
                ?>
            </ul>
        </div>
        <?php
            $profile = $entry->getCategory()->getProfile();
            $profile->getParts();
            $parts = $profile->sortParts();
            $ok = true;
            
            for ($i = 0 ; $i<count($parts); $i++) {
                $part = $parts[$i] ;
                $theme = $part->getTheme();
                
                $responseIdDao = DAOFactory::getDAO('ResponseId');
                $responseId = $responseIdDao->retrieveByEntryAndTheme($entry, $theme);
        ?>
        <hr style="margin-top:15px;" />
        <div class="theme">
            <h3>
                <?php
                Printer::display($languageManager->getText($theme->getTitle()));
                
                $buttonText = 'MODIFY_THEME';
                $buttonAction
                = "action=EditProfile&entry_id=".$entry->getId().
                "&form_id=".$theme->getId().$categoryLink;
                if (!is_null($responseId) && !$responseId->isActive() && !$part->isFree()) {
                	$buttonText = 'SHOW_THEME';
                	$buttonAction
                	= "action=ShowTheme&entry_id=".$entry->getId().
                	"&form_id=".$theme->getId().$categoryLink;
                
                	$ok = false;
                } else {
                	$ok = true;
                }
                
                if ($ok) {
                    echo HTML::link(
                        $languageManager->getText($buttonText),
                        $buttonAction,
                        array('class'=>'edit')
                    );
                }
                
                if (!$ok) {
                ?>
                <a href="<?php Printer::display(HTML::linkHref($buttonAction)); ?>" class="verrou">
					<span class="bulle-info gauche"><?php Printer::display($languageManager->getText('CLICK_TO_UNLOCK')); ?><span class="bulle-fleche"></span></span>
				</a>
                <?php
                }
                ?>
            </h3>
            <ul>
                <?php 
                if ($ok) {
                    $jsonForm = $theme->getStructure();
    
                    $responseParameters = $theme->getResponseParameters();
                    $formResponseDao = FormResponseDAO::getInstance($responseParameters);
    
                    $formResponse = null ;
                    $formValuesString = '';
    
                    if (!is_null($responseId)) {
                        
                        $formResponse =
                            $formResponseDao->retrieveByPk(
                                $responseId->getId(),
                                false,
                                true,
                                $responseId->getEntry()->getUser(),
                                $theme
                            );
    
                        if (
                            !is_null($formResponse) &&
                            !$formResponseDao->isEmpty(
                                $jsonForm,
                                $responseParameters, 
                                $formResponse
                            )
                        ) {
                            $formValuesString
                            = FormUtil::showFormValues(
                                $jsonForm,
                                $responseParameters,
                                $formResponse
                            );
                        }
                    }
                    
                    if (isNullOrEmptyString($formValuesString)) {
                    	echo "<li><strong>".HTML::link($languageManager->getText('FILL_THEME'), $buttonAction, array('class'=>'remplir'))."</strong></li>";
                    } else {
                        echo $formValuesString;
                    }
                } else {
                    echo "<li><strong>".HTML::link($languageManager->getText('UNLOCK_TO_SEE'), $buttonAction, array('class'=>'debloquer'))."</strong></li>";
                }
                ?>
            </ul>
        </div>
        <?php
            }
        }
        ?>
    </div>
</article>